/**
 * 首页类别管理初始化
 */
var Categoryclass = {
    id: "CategoryclassTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Categoryclass.initColumn = function () {
    return [
        {field: 'selectItem', checkbox: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '名称', field: 'name',  align: 'center', valign: 'middle'},
        {title: '排序', field: 'sort',  align: 'center', valign: 'middle'}
    ];
};

/**
 * 检查是否选中
 */
Categoryclass.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Categoryclass.seItem = selected[0];
        return true;
    }
};
/**
 * 批量检查是否选中
 */
Categoryclass.batchCheck = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Categoryclass.seItem = selected;
        return true;
    }
};

/**
 * 点击添加首页类别
 */
Categoryclass.openAddCategoryclass = function () {
    var index = layer.open({
        type: 2,
        title: '添加首页类别',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/categoryclass/categoryclass_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看首页类别详情
 */
Categoryclass.openCategoryclassDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '首页类别详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/categoryclass/categoryclass_update/' + Categoryclass.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除首页类别
 */
Categoryclass.delete = function () {
    if (this.batchCheck()) {
        var ajax = new $ax(Feng.ctxPath + "/categoryclass/delete", function (data) {
            Feng.success("删除成功!");
            Categoryclass.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        var ids = "";
        for (var i = 0;i < Categoryclass.seItem.length;i++) {
            if(i < Categoryclass.seItem.length - 1) {
                ids += Categoryclass.seItem[i].id + ",";
            } else {
                ids += Categoryclass.seItem[i].id;
            }
        }
        ajax.set("ids",ids);
        ajax.start();
    }
};

/**
 * 查询首页类别列表
 */
Categoryclass.search = function () {
    var queryData = {};
    queryData['name'] = $("#condition").val();
    Categoryclass.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = Categoryclass.initColumn();
    var table = new BSTable(Categoryclass.id, "/categoryclass/list", defaultColunms);
    table.setPaginationType("client");
    Categoryclass.table = table.init();
});
